﻿'Author : Ryan Lelache
'Date : 04/15/2012
'

Public Class Compare

    Private dsRoster As DataSet
    Private dsReqs As DataSet
    Private dsResults As DataSet
    Private dsPathReqs As DataSet

    Public Sub New()

        Dim dsResults As New DataSet

    End Sub


    'Compares a StudentRoster dataset and RequirementBin dataset and returns a dataset containing results of which required courses
    ' have been taken, what semester they were taken, and the resulting grade for the course
    Public Function Compare(ByVal DataSetRoster As DataSet, ByVal DataSetRequirements As DataSet, ByVal DataSetPathReqs As DataSet) As DataSet

        dsRoster = DataSetRoster
        dsReqs = DataSetRequirements
        dsResults = initResultTable()
        dsPathReqs = DataSetPathReqs.Copy

        Dim rosterSize As Integer = dsRoster.Tables(0).Rows.Count
        Dim reqSize As Integer = dsReqs.Tables(0).Rows.Count

        'This is to check if the student has taken any classes yet
        If rosterSize > 0 Then
            For i = 0 To reqSize - 1

                Dim PathReqRowID As Integer
                For x As Integer = 0 To dsPathReqs.Tables(0).Rows.Count - 1
                    If String.Equals(dsReqs.Tables(0).Rows(i)("MajDegReqID"), dsPathReqs.Tables(0).Rows(x)("MajDegReqID")) Then
                        PathReqRowID = x
                        Exit For
                    End If
                Next

                Dim row As DataRow
                row = dsResults.Tables(0).NewRow

                For j = 0 To rosterSize - 1

                    If Not dsRoster.Tables(0).Rows(j).RowState = DataRowState.Deleted Then
                        Dim reqCID As String = CStr(dsReqs.Tables(0).Rows(i).Item("CourseID"))
                        Dim rosCID As String = CStr(dsRoster.Tables(0).Rows(j).Item("CourseID"))

                        If String.Equals(reqCID, rosCID) Then

                            'Checks that a Major-Degree Requirement bin's number of required
                            ' courses has not yet been met.  If it has, the course is not
                            ' added since it is not needed.  If it has not, the course is
                            ' added and the number of required courses left is decreased
                            If CInt(dsPathReqs.Tables(0).Rows(PathReqRowID)("NumberCoursesRequired")) > 0 Then
                                row("MajDegReqID") = dsReqs.Tables(0).Rows(i).Item("MajDegReqID")
                                row("CourseID") = dsReqs.Tables(0).Rows(i).Item("CourseID")
                                row("CourseTaken") = True
                                row("Grade") = dsRoster.Tables(0).Rows(j).Item("GradeEarned")
                                row("SemesterTaken") = dsRoster.Tables(0).Rows(j).Item("SemesterTaken")

                                'Removes the specified course record from the roster DataSet
                                DataSetRoster.Tables(0).Rows(j).Delete()

                                dsPathReqs.Tables(0).Rows(PathReqRowID)("NumberCoursesRequired") = CInt(dsPathReqs.Tables(0).Rows(PathReqRowID)("NumberCoursesRequired")) - 1
                            End If

                            Exit For

                        Else
                            row("MajDegReqID") = dsReqs.Tables(0).Rows(i).Item("MajDegReqID")
                            row("CourseID") = dsReqs.Tables(0).Rows(i).Item("CourseID")
                            row("CourseTaken") = False
                            row("Grade") = ""
                            row("SemesterTaken") = ""
                        End If
                    End If

                Next

                dsResults.Tables(0).Rows.Add(row)

            Next

            Return dsResults
        Else
            'if the student has not yet completed any course
            Return Nothing
        End If
    End Function


    Public Function getPercentComplete(ByVal MajDegReqID As String, ByVal DataSetPathReqs As DataSet) As Decimal

        Dim percentComplete As Decimal

        'Get Number of Required Courses for a given MDR
        Dim numCrsReq As Integer
        For i As Integer = 0 To DataSetPathReqs.Tables(0).Rows.Count - 1
            If String.Equals(DataSetPathReqs.Tables(0).Rows(i)("MajDegReqID"), MajDegReqID) Then
                numCrsReq = CInt(DataSetPathReqs.Tables(0).Rows(i)("NumberCoursesRequired"))
            End If
        Next

        'Count number of courses taken for a given MDR
        Dim count As Integer = 0
        For i As Integer = 0 To dsResults.Tables(0).Rows.Count - 1
            If String.Equals(dsResults.Tables(0).Rows(i)("MajDegReqID"), MajDegReqID) Then
                If CBool(dsResults.Tables(0).Rows(i)("CourseTaken")) = True Then
                    count = count + 1
                End If
            End If
        Next

        'Calculate percentage completed
        If count = 0 Then
            percentComplete = 0
        ElseIf count = numCrsReq Then
            percentComplete = 100
        Else
            percentComplete = CDec(count / numCrsReq) * 100
        End If

        Return percentComplete

    End Function


    'Initializes the Results DataSet DataTable columns
    Private Function initResultTable() As DataSet

        Dim newDS As DataSet = New DataSet("Results")

        Dim newDT As DataTable = New DataTable("Results")

        Dim dataCol As DataColumn

        dataCol = New DataColumn()
        dataCol.DataType = Type.GetType("System.String")
        dataCol.ColumnName = "CourseID"
        newDT.Columns.Add(dataCol)

        dataCol = New DataColumn()
        dataCol.DataType = Type.GetType("System.Boolean")
        dataCol.ColumnName = "CourseTaken"
        newDT.Columns.Add(dataCol)

        dataCol = New DataColumn()
        dataCol.DataType = Type.GetType("System.String")
        dataCol.ColumnName = "Grade"
        newDT.Columns.Add(dataCol)

        dataCol = New DataColumn()
        dataCol.DataType = Type.GetType("System.String")
        dataCol.ColumnName = "SemesterTaken"
        newDT.Columns.Add(dataCol)

        dataCol = New DataColumn()
        dataCol.DataType = Type.GetType("System.String")
        dataCol.ColumnName = "MajDegReqID"
        newDT.Columns.Add(dataCol)

        newDS.Tables.Add(newDT)

        Return newDS

    End Function

End Class
